//
//  CenterViewController.m
//  SelfDriving
//
//  Created by apple on 15/8/25.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "CenterViewController.h"
#import "CeneterCell.h"
#import "BaseButton.h"
#import "BusinessService+FJ.h"
#import "FJModel.h"
#import "FriendDetailsMainVC.h"
#import "XhwWebViewController.h"
@interface CenterViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation CenterViewController{
    NSString *auth;
    NSString *score;
    int isinitview;//4S下图片大小处理
    NSArray *adlist;
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    
    //xhw2018-1-25加
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[AppDelegate shareDelegate].user.uid forKey:@"fuid"];
    [[BusinessService shareInstance] getUserDetail:dic success:^(id result) {
        [AppDelegate shareDelegate].user = result;
        [_table reloadData];
    } failure:^(id result) {
        NSLog(@"失败");
    }]; //xhw2018-1-25加
}
- (void)viewDidLoad {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
//    [dic setObject:@"1" forKey:@"action"];xhw11月20号改
    [dic setObject:@"1" forKey:@"type"];
    [[BusinessService shareInstance]getADlist:dic success:^(id result) {
        adlist = result;
        [_table reloadData];
    } failure:^(id result) {
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (adlist.count == 0) {
        return 7;
    }else {
        return 8;
    }
//    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 120;
    }
    //xhw2018-1-9改：bug14951(不存在广告时，不要留空白)
    else {
        if (adlist.count == 0) {
            return 45;
        }
        else {
            if (indexPath.row == 5) {
                return 70;
            }
            return 45;
        }
    }
//    if (indexPath.row == 5) {
//        return 70;
//    }
//    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CeneterCell *cell = [ tableView dequeueReusableCellWithIdentifier:@"CeneterCell"];
        cell.labelname.text = [AppDelegate shareDelegate].user.nickname;
        cell.labelnumber.text = [NSString stringWithFormat:@"一米号:%@",[AppDelegate shareDelegate].user.uid];
        [cell.imghead sd_setImageWithURL:[NSURL URLWithString:[AppDelegate shareDelegate].user.headsmall] placeholderImage:[UIImage imageNamed:@"个人中心---默认头像"]];

        cell.zlBtn.layer.cornerRadius = 5;
        [cell.zlBtn setBackgroundColor:UIColorFromRGB(0x545455)];
        cell.zlBtn.layer.borderColor = UIColorFromRGB(0xB0D97E).CGColor;
        cell.zlBtn.layer.borderWidth = 1;
        
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, cell.frame.size.height)];
        img.image = [UIImage imageNamed:@"ltbg"];
        [cell.contentView addSubview:img];
        
        [cell.contentView addSubview:cell.imghead];
        [cell.contentView addSubview:cell.zlBtn];
        [cell.contentView addSubview:cell.labelname];
        [cell.contentView addSubview:cell.labelnumber];
        if ([[AppDelegate shareDelegate].user.memberlevel intValue]!=0) {
            [cell.contentView addSubview:cell.vip];
        }
        return cell;
    }
    //xhw2018-1-9改:bug14951
    else {
        if (adlist.count == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
            if (indexPath.row == 1) {
                cell.textLabel.text = @"谁看过我";
                //            cell.detailTextLabel.text = @"22";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.imageView.image = [UIImage imageNamed:@"kg"];
            }
            if (indexPath.row == 2) {
                cell.textLabel.text = @"我的收益";
                //            cell.detailTextLabel.text = [ValidateHelper numberFormat:[NSString stringWithFormat:@"%@",[AppDelegate shareDelegate].user.money]];
                cell.detailTextLabel.text = [AppDelegate shareDelegate].user.money;
                cell.imageView.image = [UIImage imageNamed:@"sy"];
                
            }
            if (indexPath.row == 3) {
                cell.textLabel.text = @"会员中心";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.imageView.image = [UIImage imageNamed:@"hy"];
                
            }
            if (indexPath.row == 4) {
                cell.textLabel.text = @"有奖打卡";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.imageView.image = [UIImage imageNamed:@"dk"];
                
            }
            if (indexPath.row == 5) {
                cell.textLabel.text = @"设置";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.imageView.image = [UIImage imageNamed:@"set"];
                
            }
            if (indexPath.row == 6) {
                cell.textLabel.text = @"帮助";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.imageView.image = [UIImage imageNamed:@"bz"];
                
            }
            return cell;
        }
        else {
            if (indexPath.row == 5) {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
                UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, 70)];
                
                int x = 0;
                for (int i = 0; i<adlist.count; i++) {
                    UIImageView *logimg = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, SCRE_WIDTH/4, 70)];
                    FJModel *model = [adlist objectAtIndex:i];
                    [logimg sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"notimgs"]];
                    [scroll addSubview:logimg];
                    BaseButton *btn = [[BaseButton alloc]initWithFrame:logimg.frame];
                    btn.tag = indexPath.section;
                    btn.index = i;
                    btn.model = model;
                    [btn addTarget:self action:@selector(imgTouchAction:) forControlEvents:UIControlEventTouchUpInside];
                    [scroll addSubview:btn];
                    x = (i+1)*SCRE_WIDTH/4;
                }
                scroll.contentSize = CGSizeMake(x, 70); //xhw2018-1-25加
                scroll.showsHorizontalScrollIndicator = NO; //xhw2018-1-25加
                [cell.contentView addSubview:scroll];
                return cell;
            }
            else {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
                if (indexPath.row == 1) {
                    cell.textLabel.text = @"谁看过我";
                    //            cell.detailTextLabel.text = @"22";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.imageView.image = [UIImage imageNamed:@"kg"];
                }
                if (indexPath.row == 2) {
                    cell.textLabel.text = @"我的收益";
                    //            cell.detailTextLabel.text = [ValidateHelper numberFormat:[NSString stringWithFormat:@"%@",[AppDelegate shareDelegate].user.money]];
                    cell.detailTextLabel.text = [AppDelegate shareDelegate].user.money;
                    cell.imageView.image = [UIImage imageNamed:@"sy"];
                    
                }
                if (indexPath.row == 3) {
                    cell.textLabel.text = @"会员中心";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.imageView.image = [UIImage imageNamed:@"hy"];
                    
                }
                if (indexPath.row == 4) {
                    cell.textLabel.text = @"有奖打卡";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.imageView.image = [UIImage imageNamed:@"dk"];
                    
                }
                if (indexPath.row == 6) {
                    cell.textLabel.text = @"设置";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.imageView.image = [UIImage imageNamed:@"set"];
                    
                }
                if (indexPath.row == 7) {
                    cell.textLabel.text = @"帮助";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    cell.imageView.image = [UIImage imageNamed:@"bz"];
                    
                }
                return cell;
            }
        }
    }
//    else if (indexPath.row == 5){
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
//        UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, 70)];
//        int x = 0;
//        for (int i = 0; i<adlist.count; i++) {
//            UIImageView *logimg = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, SCRE_WIDTH/4, 70)];
//            FJModel *model = [adlist objectAtIndex:i];
//            [logimg sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"notimgs"]];
//            [scroll addSubview:logimg];
//            BaseButton *btn = [[BaseButton alloc]initWithFrame:logimg.frame];
//            btn.tag = indexPath.section;
//            btn.index = i;
//            btn.model = model;
//            [btn addTarget:self action:@selector(imgTouchAction:) forControlEvents:UIControlEventTouchUpInside];
//            [scroll addSubview:btn];
//            x = (i+1)*SCRE_WIDTH/4;
//        }
//        [cell.contentView addSubview:scroll];
//        return cell;
//    }else{
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
//        if (indexPath.row == 1) {
//            cell.textLabel.text = @"谁看过我";
////            cell.detailTextLabel.text = @"22";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.imageView.image = [UIImage imageNamed:@"kg"];
//        }
//        if (indexPath.row == 2) {
//            cell.textLabel.text = @"我的收益";
////            cell.detailTextLabel.text = [ValidateHelper numberFormat:[NSString stringWithFormat:@"%@",[AppDelegate shareDelegate].user.money]];
//            cell.detailTextLabel.text = [AppDelegate shareDelegate].user.money;
//            cell.imageView.image = [UIImage imageNamed:@"sy"];
//
//        }
//        if (indexPath.row == 3) {
//            cell.textLabel.text = @"会员中心";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.imageView.image = [UIImage imageNamed:@"hy"];
//
//        }
//        if (indexPath.row == 4) {
//            cell.textLabel.text = @"有奖打卡";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.imageView.image = [UIImage imageNamed:@"dk"];
//
//        }
//        if (indexPath.row == 6) {
//            cell.textLabel.text = @"设置";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.imageView.image = [UIImage imageNamed:@"set"];
//
//        }
//        if (indexPath.row == 7) {
//            cell.textLabel.text = @"帮助";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            cell.imageView.image = [UIImage imageNamed:@"bz"];
//
//        }
//        return cell;
//    }
    return nil;
}
#pragma mark 点击资料action
- (IBAction)toFriendDetailsAction:(UIButton *)sender {
    DTModel *model = [[DTModel alloc]init];
    model.uid = [AppDelegate shareDelegate].user.uid;
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = model;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    if(indexPath.row == 0){
//        DTModel *model = [[DTModel alloc]init];
//        model.uid = [AppDelegate shareDelegate].user.uid;
//        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
//                                                                     bundle:[NSBundle mainBundle]];
//        FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
//        searchResultDetailController.model = model;
//        [self.navigationController pushViewController:searchResultDetailController animated:YES];
//    }
    if (indexPath.row == 1) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"SeemeMainVC"];
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    if(indexPath.row == 2){
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MyIncomeVC"];
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    if(indexPath.row == 4){
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AwardCardViewController"];
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    //xhw2018-1-9改:
    if (adlist.count == 0) {
        if (indexPath.row == 5) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"SetVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        if (indexPath.row == 6) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"HelpVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }
    else {
        if (indexPath.row == 6) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"SetVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
        if (indexPath.row == 7) {
            UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                         bundle:[NSBundle mainBundle]];
            UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"HelpVC"];
            [self.navigationController pushViewController:searchResultDetailController animated:YES];
        }
    }
//    if (indexPath.row == 6) {
//        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
//                                                                     bundle:[NSBundle mainBundle]];
//        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"SetVC"];
//        [self.navigationController pushViewController:searchResultDetailController animated:YES];
//    }
//    if (indexPath.row == 7) {
//        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
//                                                                     bundle:[NSBundle mainBundle]];
//        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"HelpVC"];
//        [self.navigationController pushViewController:searchResultDetailController animated:YES];
//    }
    if (indexPath.row == 3) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"VipCenteVC"];
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    
}
-(void)imgTouchAction:(BaseButton *)btn{
    FJModel *model = btn.model;
//     UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"FJ"
//                                                                 bundle:[NSBundle mainBundle]];
//    WebViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"WebVC"];
//    searchResultDetailController.ntitle = @"";
//    searchResultDetailController.Url = model.url;
//    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
    
//    SVWebViewController *webC = [[SVWebViewController alloc] initWithAddress:model.url];
//    webC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:webC animated:YES];
    
    XhwWebViewController * web = [[XhwWebViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    web.urlString = model.url;
    web.ADid = model.id;
    web.title = @"广告";
    [self.navigationController pushViewController:web animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
